<template>
  <div class="user-playlists">
    <h2>我的歌单</h2>
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="!userStore.userId" class="empty-tip">
      请先登录后查看我的歌单
    </div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else class="playlists-grid">
      <div class="playlist-card create-playlist-card" @click="handleCreatePlaylist">
        <div class="hexagon">
          <div class="hex-background create-background">
            <div class="plus-icon">+</div>
          </div>
        </div>
        <div class="playlist-info">
          <h3>创建歌单</h3>
          <p>创建一个新的歌单</p>
        </div>
      </div>

      <div 
        v-for="playlist in userPlaylists" 
        :key="playlist.id" 
        class="playlist-card"
        @click="goToPlaylist(playlist.id)"
      >
        <div class="hexagon">
          <div class="hex-background">
            <img :src="playlist.cover" :alt="playlist.name">
          </div>
        </div>
        <div class="playlist-info">
          <h3>{{ playlist.name }}</h3>
          <p>{{ playlist.description }}</p>
        </div>
      </div>
    </div>

    <CreatePlaylistModal 
      v-model:visible="showCreateModal"
      @created="handlePlaylistCreated"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getUserPlaylists } from '@/api/playList';
import { useUserStore } from '@/stores/user';
import type { PlayList } from '@/api/playList';
import CreatePlaylistModal from './CreatePlaylistModal.vue';

const router = useRouter();
const userStore = useUserStore();
const userPlaylists = ref<PlayList[]>([]);
const loading = ref(true);
const error = ref('');
const showCreateModal = ref(false);

const fetchUserPlaylists = async () => {
  if (!userStore.userId) {
    error.value = '请先登录';
    loading.value = false;
    return;
  }

  try {
    const response = await getUserPlaylists();
    if (response.success && response.playlists) {
      userPlaylists.value = response.playlists;
    } else {
      error.value = response.message;
    }
  } catch (err) {
    error.value = '获取用户歌单失败';
  } finally {
    loading.value = false;
  }
};

const goToPlaylist = (id: string) => {
  router.push(`/playlist/${id}`);
};

const handleCreatePlaylist = () => {
  showCreateModal.value = true;
};

const handlePlaylistCreated = () => {
  fetchUserPlaylists();
};

onMounted(fetchUserPlaylists);
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.user-playlists {
  padding: 32px;
  border-radius: 24px;
  margin-bottom: 30px;
  overflow: hidden;
}

.playlists-grid {
  display: flex;
  gap: 35px;
  padding: 20px 0;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  flex-wrap: nowrap;

  &::-webkit-scrollbar {
    height: 4px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    transition: background 0.3s ease;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
  }

  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}

.playlist-card {
  flex: 0 0 230px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
  padding: 20px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  background-color: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  backdrop-filter: blur(10px);
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  box-shadow: 0 4px 20px var(--theme-shadow);
}

.playlist-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    145deg,
    rgba(var(--secondary-color), 0.2),
    rgba(var(--secondary-color), 0.05)
  );
  border-radius: 24px;
  opacity: 0;
  transition: all 0.4s ease;
  backdrop-filter: blur(4px);
}

.playlist-card::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 24px;
  padding: 1px;
  background: linear-gradient(
    145deg,
    rgba(255, 255, 255, 0.2),
    rgba(var(--secondary-color), 0.15),
    transparent
  );
  -webkit-mask: 
    linear-gradient(#fff 0 0) content-box, 
    linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0;
  transition: opacity 0.4s ease;
}

.hexagon {
  width: 160px;
  height: 160px;
  margin: 10px 0 25px;
  position: relative;
  clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 
    0 8px 24px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(255, 255, 255, 0.1);
}

.hex-background {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--container-background);
  position: relative;
}

.hex-background::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    145deg,
    rgba(var(--secondary-color), 0.2),
    transparent
  );
  opacity: 0;
  transition: opacity 0.4s ease;
}

.playlist-card:hover {
  transform: translateY(-8px);
  background-color: color-mix(in srgb, var(--bgc-color-2) 60%, transparent);
  box-shadow: 
    0 12px 32px rgba(0, 0, 0, 0.2),
    0 4px 24px rgba(var(--secondary-color), 0.15),
    0 0 0 1px rgba(var(--secondary-color), 0.2);
}

.playlist-card:hover::before {
  opacity: 1;
}

.playlist-card:hover::after {
  opacity: 1;
}

.playlist-card:hover .hex-background::after {
  opacity: 1;
}

.playlist-info {
  margin-top: 20px;
  width: 100%;
  z-index: 1;
  padding: 0 10px;
}

.playlist-info h3 {
  color: var(--primary-color);
  font-size: 1.1em;
  font-weight: 500;
  margin-bottom: 10px;
  letter-spacing: 0.3px;
  transition: all 0.3s ease;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.playlist-info p {
  color: rgba(var(--primary-color), 0.7);
  font-size: 0.9em;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  transition: all 0.3s ease;
}

.playlist-card:hover .playlist-info h3 {
  color: var(--secondary-color);
  transform: translateY(-2px);
}

.playlist-card:hover .playlist-info p {
  color: rgba(var(--primary-color), 0.85);
}

.empty-tip {
  text-align: center;
  padding: 64px 0;
  color: rgba(var(--primary-color), 0.8);
  font-size: 1.1em;
  letter-spacing: 0.5px;
  background: var(--card-background);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(8px);
}

.edit-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(var(--primary-color), 0.12);
  border: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: translateY(-10px);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 3;
  backdrop-filter: blur(8px);
  color: var(--primary-color);
}

.playlist-card:hover .edit-btn {
  opacity: 1;
  transform: translateY(0);
}

.edit-btn:hover {
  background: rgba(var(--primary-color), 0.2);
  transform: translateY(0) scale(1.1);
}

.playlist-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  font-size: 0.9em;
  color: rgba(var(--primary-color), 0.7);
  background: var(--container-background);
  border-radius: 12px;
  backdrop-filter: blur(8px);
  margin-top: 15px;
  transition: all 0.3s ease;
}

.song-count {
  display: flex;
  align-items: center;
  gap: 6px;
}

.playlist-card:hover .playlist-meta {
  background: var(--container-background);
  color: rgba(var(--primary-color), 0.85);
}

@media (max-width: 768px) {
  .user-playlists {
    padding: 24px 16px;
  }

  .playlist-card {
    flex: 0 0 230px;
  }
}

@media (max-width: 480px) {
  .playlist-card {
    flex: 0 0 180px;
  }

  .hexagon {
    width: 140px;
    height: 140px;
  }
}

.playlists-grid::after {
  display: none;
}

.playlists-grid:hover::after {
  display: none;
}

.create-playlist-card .hex-background {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(var(--secondary-color), 0.1);
}

.plus-icon {
  font-size: 60px;
  color: var(--secondary-color);
  font-weight: 300;
  transition: all 0.3s ease;
}

.create-playlist-card:hover .plus-icon {
  transform: scale(1.1);
  color: var(--primary-color);
}

.create-playlist-card:hover .hex-background {
  background: rgba(var(--secondary-color), 0.3);
}

.create-playlist-card .playlist-info h3 {
  color: var(--secondary-color);
}

.create-playlist-card:hover .playlist-info h3 {
  color: var(--primary-color);
}

.create-playlist-card .playlist-info p {
  color: rgba(var(--secondary-color), 0.7);
}

.create-playlist-card:hover .playlist-info p {
  color: rgba(var(--primary-color), 0.85);
}
</style> 